<?php

namespace App\Http\Controllers\Client;

use App\Services\Tbk\PddService;
use App\Utils\ApiResponse;
use App\Utils\PriceUtis;
use Illuminate\Http\Request;

class ProductController extends ClientBase
{
    /**
     * 设置商品链接
     */
    public function setLink(Request $request)
    {
        $data = $request->decrypted_data;
        $keyword = trim((string) $data->keyword);
        $pddService = new PddService();
        if ($keyword != '') {
            $goodsInfo = $pddService->searchGoodsInfoByKeyword($keyword, env('PDD_PID'), $this->customParamJson);
            if (isset($goodsInfo['goods_search_response']['goods_list'][0]['goods_name'])) {
                $popupInfo = [
                    'title'      => $goodsInfo['goods_search_response']['goods_list'][0]['goods_name'],
                    'image'      => $goodsInfo['goods_search_response']['goods_list'][0]['goods_image_url'],
                    'goods_sign' => $goodsInfo['goods_search_response']['goods_list'][0]['goods_sign'],
                    'channel'    => '拼多多商品',
                ];
                ApiResponse::success([
                    'product_info' => $popupInfo
                ]);
            }
            ApiResponse::fail("该商品没有找到返利信息");
        }
    }

    /**
     * 获取商品详情
     */
    public function getProductInfo(Request $request)
    {
        $data = $request->decrypted_data;
        $goodsSign = trim((string) $data->goods_sign);
        if ($goodsSign != "") {
            $pddService = new PddService();
            $goodsInfo = $pddService->searchGoodsInfoByKeyword($goodsSign, env('PDD_PID'), $this->customParamJson);
            if (isset($goodsInfo['goods_search_response']['goods_list'][0]['goods_name'])) {
                $productInfo = $goodsInfo['goods_search_response']['goods_list'][0];
                // 转链结果
                $updateLinkRes = $pddService->updateLink($goodsSign, env('PDD_PID'), $this->customParamJson);
                $popupInfo = [
                    'title'       => $productInfo['goods_name'],
                    'image'       => $productInfo['goods_image_url'],
                    'goods_sign'  => $productInfo['goods_sign'],
                    'channel'     => '拼多多商品',
                    'h5_link'     => $updateLinkRes['goods_promotion_url_generate_response']['goods_promotion_url_list'][0]['mobile_url'],
                    'we_app_info' => $updateLinkRes['goods_promotion_url_generate_response']['goods_promotion_url_list'][0]['we_app_info'],
                ];
                // 券后价格
                $popupInfo['price'] = sprintf("%.2f", PriceUtis::toYuan($productInfo['min_group_price'])); // 最低拼团价
                if ($productInfo['has_coupon']) {
                    // 最低拼团价-优惠券价格
                    $popupInfo['price'] = sprintf("%.2f",
                        PriceUtis::toYuan(bcsub($productInfo['min_group_price'], $productInfo['coupon_discount'])));
                }
                $popupInfo['price'] = handle0($popupInfo['price']);
                // 预估佣金 = 券后价格 * 佣金千分比
                $promotion = sprintf("%.2f",
                    bcdiv(bcmul($popupInfo['price'], $productInfo['promotion_rate']), 1000, 6));
                $promotionFen = PriceUtis::toFen($promotion); // 分
                // 扣除平台抽成
                $platPrice = PriceUtis::platPrice($promotionFen); // 分
                $popupInfo['promotion'] = handle0(sprintf("%.2f", PriceUtis::toYuan(bcsub($promotionFen, $platPrice))));
                ApiResponse::success([
                    'product_info' => $popupInfo
                ]);
            }
            ApiResponse::fail("没有找到该商品");
        }
    }

    /**
     * 商品列表
     */
    public function getProductList(Request $request)
    {
        $data = $request->decrypted_data;
        $page = 1;
        if (isset($data->page) && (int) $data->page > 0) {
            $page = $data->page;
        }
        $pageSize = 10;
        if (isset($data->page_size)) {
            if ((int) $data->page_size > 10 && (int) $data->page_size <= 100) {
                $pageSize = $data->page_size;
            }
        }
        $pddService = new PddService();
        $res = $pddService->getProductList(env('PDD_PID'), $this->customParamJson, "10564,11879,4,7,10851,24", $page, $pageSize);
        $productList = $res['goods_search_response']['goods_list'];
        $return = [];
        foreach ($productList as $product) {
            $return[] = [
                'id'           => $product['goods_id'],
                'title'        => $product['goods_name'],
                'img'          => $product['goods_thumbnail_url'],
                'price'        => handle0(sprintf("%.2f",
                    PriceUtis::toYuan($product['min_group_price'] - $product['coupon_discount']))), // 券后价
                'goods_sign'   => $product['goods_sign'],
                'scribe_price' => handle0(sprintf("%.2f", PriceUtis::toYuan($product['min_group_price']))) // 划线价
            ];
        }
        ApiResponse::success([
            'product_list' => $return,
            'page'         => $page,
            'page_size'    => $pageSize
        ]);
    }
}
